package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.PlayerCharacter;
import model.Stats;

public class StatPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	public StatPanel(final PlayerCharacter the_character) {
		super();
		
		JPanel stats = new JPanel();
		JPanel statBody = new JPanel();
		LabelPanel statHeader = new LabelPanel();
		
		GridLayout statButtonLayout = new GridLayout(6,6);
		statButtonLayout.setHgap(20);
		statBody.setLayout(statButtonLayout);
		stats.setLayout(new BoxLayout(stats, BoxLayout.Y_AXIS));
		
		final JLabel pointCounter = new JLabel("32 Points");
		
		final Dimension buttonSize = new Dimension(60,60);
		
		final StatButton minusButtons[] = new StatButton[6];
		final StatButton plusButtons[] = new StatButton[6];
		final StatLabel statLabels[] = new StatLabel[6];
		final ModifierLabel modifiers[] = new ModifierLabel[6];
		final JCheckBox racials[] = new JCheckBox[6];
		final JLabel pointReqs[] = new JLabel[6];
		
		Stats[] statArray = new Stats[6];
		for(int i=0;i<6;i++) {
		    statArray[i] = Stats.getStat(i);
		}
		final JComboBox<Stats> reducedStat = new JComboBox<Stats>(statArray);
		
		for(int i=0; i< 6; i++){
		    	modifiers[i] = new ModifierLabel(i);
		    	modifiers[i].calculateModifier(the_character.attributes[i]);
		    	
			final StatButton minus = new StatButton("-",i);
			final StatButton plus = new StatButton("+",i);
			final StatLabel statLabel = new StatLabel(the_character,i);
			final JLabel pointReq = new JLabel("" + the_character.getRequiredPoints(i));
			final JCheckBox racial = new JCheckBox();
			
			minusButtons[i] = minus;
			plusButtons[i] = plus;
			statLabels[i] = statLabel;
			
			//minus button
			minus.setMaximumSize(buttonSize);
			minus.setEnabled(false);
			minus.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent datEvent) {
					the_character.decreaseAttribute(Stats.getStat(minus.stat));
					for(int j=0; j<6; j++) {
					    minusButtons[j].setEnabled(the_character.canDecreaseAttribute(Stats.getStat(j)));
					    plusButtons[j].setEnabled(the_character.canIncreaseAttribute(Stats.getStat(j)));
					}
					pointReq.setText("" + the_character.getRequiredPointsbyStat(plus.stat));
					modifiers[minus.stat].calculateModifier(the_character.attributes[minus.stat]);
					statLabel.refresh();
					pointCounter.setText("" + the_character.points + " points");
					reducedStat.setEnabled(the_character.canSwitchReducedAttribute());
				}
				
			});
			
			JPanel minusIsolator = new JPanel();
			minusIsolator.setLayout(new BoxLayout(minusIsolator,BoxLayout.LINE_AXIS));
			minusIsolator.add(minus);
			statBody.add(minusIsolator);
			
			//stat label
			statBody.add(statLabel);		
			
			//plus button
			plus.setMaximumSize(buttonSize);
			plus.setEnabled(true);
			plus.addActionListener(new ActionListener(){
			    @Override
			    public void actionPerformed(ActionEvent datEvent) {
				the_character.increaseAttribute(Stats.getStat(plus.stat));
				for(int j=0; j<6; j++) {
				    minusButtons[j].setEnabled(the_character.canDecreaseAttribute(Stats.getStat(j)));
				    plusButtons[j].setEnabled(the_character.canIncreaseAttribute(Stats.getStat(j)));
				}
				pointReq.setText("" + the_character.getRequiredPointsbyStat(plus.stat));
				modifiers[plus.stat].calculateModifier(the_character.attributes[plus.stat]);
				statLabel.refresh();
				pointCounter.setText("" + the_character.points + " points");
				reducedStat.setEnabled(false);
				System.out.println("");
			    }
			});
			
			JPanel plusIsolator = new JPanel();
			plusIsolator.setLayout(new BoxLayout(plusIsolator,BoxLayout.LINE_AXIS));
			plusIsolator.add(plus);
			statBody.add(plusIsolator);
			
			//required point panel
			pointReqs[i] = pointReq;
			statBody.add(pointReq);

			//modifier column
			statBody.add(modifiers[i]);
			
			//racial bonus
			racials[i] = racial;
			racial.addActionListener(new ActionListener() {
			    @Override
			    public void actionPerformed(ActionEvent e) {
				the_character.setRacialBonus(minus.stat,racial.isSelected());
				statLabels[minus.stat].refresh();
				pointReq.setText("" + the_character.getRequiredPointsbyStat(plus.stat));
				modifiers[plus.stat].calculateModifier(the_character.attributes[plus.stat]);
				reducedStat.setEnabled(the_character.canSwitchReducedAttribute());
			    }
			});
			
			statBody.add(racial);
		} //end for loop
		
		stats.add(statHeader);
		stats.add(statBody);
		
		JPanel sidePanel = new JPanel();
		BoxLayout sidePanelLayout = new BoxLayout(sidePanel, BoxLayout.Y_AXIS);
		sidePanel.setLayout(sidePanelLayout);
		
		pointCounter.setAlignmentX(Component.CENTER_ALIGNMENT);
		JLabel dumpStatLabel = new JLabel("Dump Stat  ");
		dumpStatLabel.setAlignmentX(CENTER_ALIGNMENT);
		
		sidePanel.add(pointCounter);
		sidePanel.add(dumpStatLabel);
		
		reducedStat.addActionListener(new ActionListener(){
		    public void actionPerformed(ActionEvent e) {
			the_character.setReducedAttribute(Stats.getInt((Stats)reducedStat.getSelectedItem()));
			for(int j=0;j<6;j++) {
			    statLabels[j].refresh();
			    modifiers[j].calculateModifier(the_character.attributes[j]);
			}
		    }
		});
		reducedStat.setMaximumSize(new Dimension(80,25));
		sidePanel.add(reducedStat);
		
		//reset button
		JButton reset = new JButton("Reset");
		reset.addActionListener(new ActionListener(){
		    public void actionPerformed(ActionEvent e) {
			the_character.reset();
			for(int i=0;i<6;i++) {
			    minusButtons[i].setEnabled(false);
			    plusButtons[i].setEnabled(true);
			    statLabels[i].refresh();
			    modifiers[i].calculateModifier(the_character.attributes[i]);
			    racials[i].setSelected(false);
			    pointReqs[i].setText("" + the_character.getRequiredPointsbyStat(i));
			}
			reducedStat.setEnabled(true);
			reducedStat.setSelectedIndex(0);
			pointCounter.setText("" + the_character.points + " points");
		    }
		});
		reset.setAlignmentX(CENTER_ALIGNMENT);
		
		sidePanel.add(reset);
		setLayout(new BorderLayout());
		add(stats,BorderLayout.CENTER);
		add(sidePanel,BorderLayout.EAST);
		
		
	}
}
